﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using MarsExplorerWebSite.Model;

namespace MarsExplorerWebSite.Functions
{
    public class setTeamMining : FunctionBase
    {
        private MarsExplorerEntities dbContext;
        private MarsExplorerEntities dbContextAux;
        public int teamID { get; set; }
        int combustivel = 0, oxigenio = 0, comida = 0, agua = 0, medico = 0, escudo = 0, fato = 0;
        int _combustivel = 0, _oxigenio = 0, _comida = 0, _agua = 0, _medico = 0, _escudo = 0,_fato = 0;
        public setTeamMining(HttpContext context) : base(context)
        {
            InitializeProperties();
        }

        public override string Execute()
        {
            Team t = (from x in dbContext.Teams where x.TeamID == teamID select x).FirstOrDefault();
            if (t.RouteID == 20)
            {
                if (checkMineAllowed())
                {
                    //removeResources();
                    t.Mining = 1;
                    t.canMove = 1;
                    dbContext.SaveChanges();
                    return "ok";
                }
                else
                {
                    t.Mining = 0;
                    dbContext.SaveChanges();
                    return "Não alocou os recursos necessários";
                }
            }
            else
            {
                return "Não pode minar aqui";
            }
        }

        private string removeResources()
        {
            var tr = (from x in dbContextAux.TeamsResources where x.TeamID == teamID select x).ToList();
            var tt = (from z in dbContextAux.TeamsTurns where z.TeamID == teamID select z).ToList(); 
            foreach (TeamsTurn f in tt)
            {
                f.Value = 0;
            }
            foreach (TeamsResource r in tr)
            {
                switch (r.ResourceID)
                {
                    case 1:
                        r.Value -= agua;
                        break;
                    case 2:
                        r.Value -= combustivel;
                        break;
                    case 3:
                        r.Value -= comida;
                        break;
                    case 4:
                        r.Value -= oxigenio;
                        break;
                    case 5:
                        r.Value -= medico;
                        break;

                    case 7:
                        r.Value -= escudo;
                        break;
                    case 8:
                        r.Value -= fato;
                        break;
                    default:
                        break;
                }
                dbContextAux.SaveChanges();
            }
            return "ok";
        }


        private Boolean checkMineAllowed()
        {
            getNeededResources();
            getAllocatedResources();

            if (agua >= _agua && combustivel >= _combustivel && oxigenio >= _oxigenio && comida >= _comida && fato>=_fato)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        private string getNeededResources()
        {
            _agua = 1;
            _comida = 1;
            _oxigenio = 1;
            _combustivel = 1;
            _fato = 1;
                      
            return "ok";
        }

        private string getAllocatedResources()
        {
            var tt = from tts in dbContext.TeamsTurns where tts.TeamID == teamID select tts;

            foreach (TeamsTurn j in tt)
            {
                switch (j.ResourceID)
                {
                    case 1:
                        agua = j.Value;
                        break;
                    case 2:
                        combustivel = j.Value;
                        break;
                    case 3:
                        comida = j.Value;
                        break;
                    case 4:
                        oxigenio = j.Value;
                        break;
                    case 5:
                        medico = j.Value;
                        break;
                    case 7:
                        escudo = j.Value;
                        break;
                    case 8:
                        fato = j.Value;
                        break;
                    default:
                        break;
                }
            }
            return "ok";
        }

        public override void InitializeProperties()
        {
            try
            {
                teamID = Convert.ToInt32(Parameters["teamid"]);
                dbContext = new MarsExplorerEntities();
                dbContextAux = new MarsExplorerEntities();
            }
            catch (Exception e)
            {
                string message = "";
                #if DEBUG
                message += " : " + e.ToString();
                #endif
            }
        }
    }
}